from django import http
from django.views import View
from django_redis import get_redis_connection

from libs.captcha.captcha import captcha
from utils import constants


class ImageCodeView(View):
    """图形验证码"""

    def get(self, request, uuid):
        # 生成图片验证码
        _, text, image = captcha.generate_captcha()

        # 保存图片验证码
        redis_conn = get_redis_connection('code')
        redis_conn.setex('img_%s' % uuid, constants.IMAGE_CODE_REDIS_EXPIRES, text)

        # 响应图片验证码
        return http.HttpResponse(image, content_type='image/jpeg')
